package com.zbkj.service.manager.impl;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.ObjectMetadata;
import com.zbkj.service.config.TencentOssProperties;
import com.zbkj.service.manager.TencentManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;


@Slf4j
@Component
public class TencentManagerImpl implements TencentManager {

    @Resource
    private COSClient cosClient;

    @Resource
    private TencentOssProperties tencentOssProperties;

    @Override
    public void uploadFile(byte[] bytes, String path) {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        this.uploadFile(inputStream, path);
    }

    @Override
    public void uploadFile(InputStream stream, String path) {
        cosClient.putObject(tencentOssProperties.getBucketName(), path, stream, new ObjectMetadata());
    }

    @Override
    public String imagePath(String path) {
        if (StringUtils.isBlank(path)) {
            return null;
        }
        if (path.endsWith("/")) {
            return tencentOssProperties.getUrl() + path;
        }
        return tencentOssProperties.getUrl() + "/" + path;
    }

}
